﻿namespace AgileBoard.Infrastructure.Data.Repositories
{
    using System;
    using System.Collections.Generic;
    using Domain;
    using Domain.DataInterfaces;
    using NHibernate;
    using SharpArch.Data.NHibernate;

    public class FeatureRepository : Repository<Feature>, IFeatureRepository
    {
        public IList<Feature> FindByProject(Guid ProjectID)
        {
            IQuery query =
                Session.CreateQuery(
                    "from Feature F, Release R, Project P where F.ReleaseID = R.ReleaseID and R.ProjectID = P.ID and P.ID='" +
                    ProjectID + "'");

            //foreach (object obj in (object[])query.List()[0])
            //{
            //    Feature feature = obj as Feature;
            //    if(feature != null
            //}
            IList<Feature> features = query.List<Feature>();

            if (features.Count > 0)
            {
                features = ((object[]) query.List()[0])[0] as List<Feature>;

                if (features == null)
                {
                    features = new List<Feature>();
                    var feature = ((object[]) query.List()[0])[0] as Feature;
                    if (feature != null)
                        features.Add(feature);
                }
            }

            return features;
        }

        public IList<Feature> FindByRelease(string releaseId)
        {
            return Session.CreateQuery("from Features where ReleaseID = :releaseId")
                .SetParameter("releaseId", releaseId)
                .List<Feature>();
        }
    }
}